<?php

defined('IN_IA') or exit('Access Denied');

$dos = array_keys(getOperate());
$do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'list';
$table = 'sms_finance';
$status = getBizStatus();
if ($do == 'list') {
    $_W['page']['title'] = '管理库款划拨信息 - 库款划拨列表';
    // 显示所有发文机关
    $files = pdo_fetchall("SELECT * FROM " . tablename('dictionary') . " WHERE parent = '2'");
    $fileyear = trim($_GPC['fileyear']);
    $fileid = trim($_GPC['fileid']);
    $condition = "  WHERE 1 = 1 ";
    if (!empty($fileyear)) {
        $condition .= " AND fileyear LIKE '%{$fileyear}%'";
        $_GET['fileyear'] = $_GPC['fileyear'];
    }
    if (!empty($fileid)) {
        $condition .= " AND fileid LIKE '%{$fileid}%'";
        $_GET['fileid'] = $_GPC['fileid'];
    }
    $file = trim($_GPC['file']);
    if (!empty($file)) {
        $condition .= " AND agency = '{$file}'";
        $_GET['agency'] = $file;
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10; //ORDER BY createtime DESC
    $list = pdo_fetchall("SELECT * FROM " . tablename($table) . $condition .' ORDER BY createtime desc' . '   LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($table) . $condition);
    $pager = pagination($total, $pindex, $psize);
    template('sms/file_list');
} else {
    checkModuleRight($user, 'finance', $do);
}
if ($do == 'add' || $do == 'edit') {
    if ($do == 'edit') {
        $_W['page']['title'] = '管理库款划拨信息 - 修改库款划拨';
    } else {
        $_W['page']['title'] = '管理库款划拨信息 - 增加库款划拨';
    }
    $id = intval($_GPC['id']);
    if (!empty($id)) {
        $row = getFinanceById($id);
        if(empty($row)){
            message('无效的数据参数！', url('finance/finance/list'), 'error');
        }
        //检查数据是否有权操作
        if($do == 'edit'){
            checkRecodeRight($row, $user, 'edit');
        }
    }

    if (isset($_GPC['step'])) {
        $step = intval($_GPC['step']);
    } else {
        $step = empty($row) ? 1 : 2;
        $_GPC['step'] = $step;
    }

    if (checksubmit('submit')) {
        if ($step == '2') {
            //没有记录，查询对应的记录
            if (empty($row)) {
                $cates = $_GPC['cates'];
                $fileyear = $_GPC['fileyear'];
                $fileid = $_GPC['fileid'];
                $row = getFinance($cates, $fileyear, $fileid);
            }
            if (empty($row)) {
                $row = array(
                    'agency' => $cates,
                    'fileyear' => $fileyear,
                    'fileid' => $fileid,
                    'firstpage' => '',
                    'signpage' => '',
                );
            }else{
                if($do=='add'&&$row['status']!=PASS){
                    message('当前数据未成功划拔前，不能再次增加！', url('finance/finance/list'), 'error');
                }
                $id=$row['id'];
            }
        }
        if ($step == '3') {
            //界面输入数据
            $data = array(
                'agency' => $_GPC['agency'],
                'fileyear' => $_GPC['fileyear'],
                'fileid' => $_GPC['fileid'],
                'totalmoney' => $_GPC['totalmoney'],
                'money' => $_GPC['money'],
                'firstpage' => $_GPC['firstpage'],
                'signpage' => $_GPC['signpage'],
                'sid' => intval($_GPC['sid']),
            );
            $id = saveFinance($data, $row);
        }
        if (($step == 4) && ($_GPC['submit'] == 'audit')) {

            if ($row['status'] != PASS) {
                $data['status'] = AUDIT;
                pdo_update('sms_finance', $data, array('id' => $id));
                pdo_update('finance_detail', $data, array('id' => $row['detailid']));
                message('数据更新成功！', url('finance/finance/list'), 'success');
            }
        }
    }

    if ($step == 1) {
        $files = pdo_fetchall("SELECT * FROM " . tablename('dictionary') . " WHERE parent = '2'");
    }
    if ($step == 2) {
        $item = getFinanceLastDetail($id);

        $details = getFinanceDetails($id, PASS);
        load()->func('tpl');
    }
    if ($step == 3) {
        $item = getFinanceLastDetail($id);
        $list = getFinanceDetails($id, PASS);
    }
    template('sms/finance_post');
}

if ($do == 'audit' ) {
    $id = intval($_GPC['id']);
    $row = getFinanceById($id);
    if ($do == 'audit') {
        $item = getFinanceLastDetail($row);
        $list = getFinanceDetails($id, PASS);
    } else {
        $list = getFinanceDetails($id);
    }
    if (empty($row)||$row['status']!=AUDIT) {
        message('数据不存在或非提交审核状态！');
    } else {
        checkRecodeRight($row, $user, 'audit');
    }
    if (($do == 'audit') && checksubmit()) {
        //处理审核
        if(!empty($item)&&($item['status']==AUDIT)){
            if ($_GPC['submit'] == 'pass') {
                $status = PASS;
            } else {
                $status = NOPASS;
            }
            $data = array(
                'status' => $status,
                'auditor' => $_W['username'],
                'auditid' => $_W['uid'],
                'changetime' => TIMESTAMP,
            );
            pdo_update('finance_detail', $data, array('id' => $item['id']));
            $data=array(
                'status' => $status,
            );
            if(($status==PASS)&&($row['detailid']>0)){
                //处理已拔和剩余
                $data['detailid']=0;
                $data['money']=$row['money']+$item['money'];
                $data['num']=$row['num']+1;

            }
            pdo_update('sms_finance', $data, array('id' => $item['parentid']));
            message('审核处理完成！', url('finance/finance/list'), 'success');
        }
    }
    template('sms/file_audit');
}
if( $do == 'disp'){
    $id = intval($_GPC['id']);
    $row = getFinanceById($id);
    $list = getFinanceDetails($id);
    template('sms/file_audit');
}

if ($do == 'dele') {
    checkModuleRight($user, 'finance', 'delete');
    $id = intval($_GPC['id']);
    if ($id > 0) {
        $row = getFinanceById($id);
        if($row['num']==0){

        }else
        {}
        if (!in_array($row['status'], array(PASS, AUDIT))) {
            pdo_update($table, array('status' => DELETED, 'changetime' => TIMESTAMP), array('id' => $row['id']));
            message('数据删除成功！', url('finance/finance/list'), 'success');
        }else{
            message('此数据禁止删除！', url('finance/finance/list'), 'error');
        }
    }
}
